package bd403.UIComponents;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import bd403.Backend.Drink;
import bd403.Backend.DrinkStorage;
import bd403.namespace.BDActivity;
import bd403.namespace.R;
import bd403.utils.DrinkAdapter;

/* Main page that the application initially opens with. Welcomes the user, displays top
 * drinks for the application, and the users favorite drinks.
 * Main author: Darren
 */

public class HomeActiv extends Activity {
	private ArrayList<Drink> selectedDrinks;
	private DrinkAdapter m_adapter;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		selectedDrinks = new ArrayList<Drink>();
		selectedDrinks.clear();
        
		ListView selectedDrinkLV = (ListView) findViewById(R.id.selectedListView);
		m_adapter = new DrinkAdapter(this, R.layout.drinkitem, selectedDrinks);
		selectedDrinkLV.setAdapter(m_adapter);
		
		selectedDrinkLV.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				onClick(view, position, id);
			}	
		});
		
		// Setup listener to reload list when radio buttons are clicked.
		final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.home_radiogroup);
		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
	            @Override
	            public void onCheckedChanged(RadioGroup RG, int checkID) {
	            	//bdSyncTask serverConnect = new bdSyncTask();
	        		//serverConnect.execute();
	            	populateList();
	            }
        	});
		
		// Welcome the user
		TextView tv = (TextView) findViewById(R.id.welcomeLabel);
		DrinkStorage ds = BDActivity.getStorage();
		tv.setText(getString(R.string.welcome_beforename) + ds.getUsername() + 
				getString(R.string.welcome_aftername));
	}
	
	/* Repopulate the top drinks on resume.
	 */
	@Override
	protected void onResume() {
		populateList();
		super.onResume();
	}
	
	/* Looks at the radio button selection and fills selectedDrinks using
	 * the appropriate Iterator<Drink>.
	 */
	private void populateList() {
		// Clear the old drinks and reload new top and fav drinks from DrinkStorage
	    selectedDrinks.clear();
	    
	    DrinkStorage ds = BDActivity.getStorage();
	    
	    final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.home_radiogroup);
	    RadioButton checkedRadioButton = 
	    		(RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
	    
	    if (checkedRadioButton.getId() == R.id.top_drinks) {
	    	Iterator<Drink> topIterator = ds.getTopDrinks();
	    	while (topIterator.hasNext()) {
	    		selectedDrinks.add(topIterator.next());
	    	}
	    } else {
	    	Iterator<Drink> selectedIterator = ds.getFavDrinks();
	    	TextView emptyText = (TextView) findViewById(R.id.empty);
		    if (selectedIterator.hasNext()) {
	    		emptyText.setText("");
	    		while (selectedIterator.hasNext()) {
	    			selectedDrinks.add(selectedIterator.next());
	    		}
	    	} else {
	    		emptyText.setText(getString(R.string.no_favorites));
	    	}
	    }
		m_adapter.notifyDataSetChanged();
	}
	
	/* Setup listener for a click on an individual drink.
	 */
	protected void onClick(View v, int position, long id) {
        Intent i = new Intent(this, DrinkViewActiv.class);
      
        Drink d = m_adapter.getItem(position);
        
        /* Data bind Drink d to BDStorage to pass to another Activity */
        DrinkStorage ds = BDActivity.getStorage();
        ds.setCurrentDrink(d);
        
        startActivity(i);
    }
}